/*
 * Signal.java
 *
 * Created on Jan 13, 2012
 *
 * Copyright (c) 2011 Artur Rataj.
 *
 * This code is distributed under the terms of the GNU Library
 * General Public License, either version 3 of the license or, at
 * your option, any later version.
 */

package pl.gliwice.iitis.spatialnn.propagate;

import java.util.*;

import pl.gliwice.iitis.spatialnn.network.Membrane;
import pl.gliwice.iitis.spatialnn.network.MembraneType;
import pl.gliwice.iitis.spatialnn.network.Neuron;

/**
 * A signal at inputs and an output of a single neuron.
 * 
 * @author Artur Rataj
 */
public class Signal {
    /**
     * The neuron, through which propagates the signal.
     */
    public Neuron neuron;
    /**
     * Subsequent values of inputs in <code>neuron.inputs</code>.
     */
    public double[] in;
    /**
     * Outputs of this signal.
     */
    List<SignalOutput> outputs;
    
    /**
     * Creates a new instance of <code>Signal</code>.
     * 
     * @param neuron the neuron, through which propagates the signal
     */
    public Signal(Neuron neuron) {
        this.neuron = neuron;
        in = new double[this.neuron.inputs.length];
        outputs = new LinkedList<>();
    }
    /**
     * Sets another membrane as an output of this signal.
     * 
     * @param membrane membrane
     * @param connectionIndex index of a connection in the membrane
     */
    public void addOut(Membrane membrane, int connectionIndex) {
        outputs.add(new SignalOutput(membrane, connectionIndex));
    }
    /**
     * Sets another signal as an output of this signal.
     * 
     * @param signal the other signal
     * @param inIndex index of an input in the other signal
     */
    public void addOut(Signal signal, int inIndex) {
        if(signal.neuron.layerNum >= neuron.layerNum)
            throw new RuntimeException("invalid order of propagation");
        outputs.add(new SignalOutput(signal, inIndex));
    }
    @Override
    public String toString() {
        String out = neuron.key + "[";
        for(double i : in)
            out += i + ",";
        out = out.substring(0, out.length() - 1) + "]";
        return out;
    }
}
